package org.itboys.basetemplate.web;

import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.itboys.admin.entity.BusinessCenter;
import org.itboys.admin.entity.BusinessCenterUser;
import org.itboys.admin.service.BusinessCenterService;
import org.itboys.admin.service.BusinessCenterUserService;
import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.member.entity.Member;
import org.itboys.member.service.MemberService;
import org.itboys.mongodb.utils.page.AceJsonPageUtils;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.mongodb.utils.query.QueryParamUtils;
import org.itboys.seller.entity.Seller;
import org.itboys.seller.entity.SellerUser;
import org.itboys.seller.service.SellerService;
import org.itboys.seller.service.SellerUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 会员管理
 * 
 * @author weisky
 *
 */
@RestController
@RequestMapping("/ace/member")
public class MemberController extends BaseController {

	@Autowired
	private MemberService memberService;
	@Autowired
	private BusinessCenterService businessCenterService;
	@Autowired
	private BusinessCenterUserService businessCenterUserService;
	@Autowired
	private SellerUserService sellerUserService;
	@Autowired
	private SellerService sellerService;

	/**
	 * 点击会员管理跳转页面
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping("/toVM")
	public ModelAndView listvm(HttpServletRequest request, HttpServletResponse response, Model model) {
        return new ModelAndView("/member/member");
	}

	/**
	 * 分页查询
	 *
	 * @return
	 */
	@RequestMapping("/list")
	public void list(@Param("type")Integer type, @Param("nickName")String nickName, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> param = QueryParamUtils.builderQueryMap(request);
		param.put("isBuyer",1);
		if(StringUtils.isNotBlank(nickName)) {
			param.put("nickname_contains", nickName);
		}
		if(type != null) {
			if(type == 1) {
				BusinessCenterUser user = businessCenterUserService.getById(AdminSessionHolder.getAdminUserId());
				if(user != null) {
					param.put("centerId",user.getCenterId());
				}
			} else if(type == 2) {
				SellerUser user = sellerUserService.getById(AdminSessionHolder.getAdminUserId());
				if(user != null) {
					param.put("sellerId",user.getSellerId());
				}
			}
		}
        Page<Member> page = memberService.pageQuery(param, request);
		for (Member member : page.getData()) {
			if(member.getCenterId() != null) {
				BusinessCenter center = businessCenterService.getById(member.getCenterId());
				if(center != null) {
					member.setCenterName(center.getCompanyName());
				}
			}
			if(member.getSellerId() != null) {
				Seller seller = sellerService.getById(member.getSellerId());
				if(seller != null) {
					member.setSellerName(seller.getName());
				}
			}
			if(member.getRecId() != 0 && member.getRecId() != null) {
				Member member1 = memberService.getById(member.getRecId());
				if(member1 != null) {
					member.setRecName(member1.getNickname());
				}
			}
		}
		AceJsonPageUtils.renderJsonPage(page.getTotal(), page.getData(), response);
	}

}
